package com.signway.javaui.slice;

import com.signway.javaui.ResourceTable;
import com.signway.javaui.define.CommonData;
import com.signway.javaui.utils.SwLog;
import com.signway.javaui.utils.SwToast;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;

public class LayoutAbilitySlice extends AbilitySlice {
    public static final String ABILITY_NAME = CommonData.LAYOUT_ABILITY;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_layout);

        initView();
    }

    private void initView() {
        Button btnDirection = (Button) findComponentById(ResourceTable.Id_directional_layout); /* 定向布局 */
        Button btnDependent = (Button) findComponentById(ResourceTable.Id_dependent_layout); /* 依赖布局 */
        Button btnPosition = (Button) findComponentById(ResourceTable.Id_positional_layout); /* 位置布局 */
        Button btnTable = (Button) findComponentById(ResourceTable.Id_table_layout); /* 表格布局 */
        Button btnAdaptiveBox = (Button) findComponentById(ResourceTable.Id_adaptive_box_layout); /* 自适应布局 */
        Button btnStack = (Button) findComponentById(ResourceTable.Id_stack_layout); /* 堆叠布局 */

        btnDirection.setClickedListener(component -> startAbilitySlice(CommonData.DIRECTIONAL_LAYOUT));
        btnDependent.setClickedListener(component -> startAbilitySlice(CommonData.DEPENDENT_LAYOUT));
        btnPosition.setClickedListener(component -> startAbilitySlice(CommonData.POSITION_LAYOUT));
        btnTable.setClickedListener(component -> startAbilitySlice(CommonData.TABLE_LAYOUT));
        btnAdaptiveBox.setClickedListener(component -> startAbilitySlice(CommonData.ADAPTIVE_BOX_LAYOUT));
        btnStack.setClickedListener(component -> startAbilitySlice(CommonData.STACK_LAYOUT));
    }

    private void startAbilitySlice(String abilitySliceName) {
        if (abilitySliceName.isEmpty()) {
            SwToast.showToast(this, "开发中...");
            SwLog.info("开发中...");
            return;
        }
        SwLog.info("Start Slice is " + abilitySliceName);
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ABILITY_NAME)
                .withAction(abilitySliceName)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
